What are decorators in Python?
What are decorators in Python?
Ravi Vishwakarma is a dedicated Software Developer with a passion for crafting efficient and innovative solutions. With a keen eye for detail and years of experience, he excels in developing robust software systems that meet client needs. His expertise spans across multiple programming languages and technologies, making him a valuable asset in any software development project.
ICSM Computer
02-Jun-2025Decorators in Python are a powerful and elegant way to modify or enhance the behavior of functions or methods without changing their actual code.
What is a decorator?
@decorator_namesyntax placed just above the function definition.Why use decorators?
Basic example of a decorator
Output:
How it works:
@my_decoratoris syntactic sugar for:When you call
say_hello(), it actually callswrapper(), which runs extra code around the original function.Decorators with arguments
To decorate functions that take parameters, your wrapper function should accept
*argsand**kwargs:Summary
@staticmethod,@classmethod, and@property.